// App Engine Example
resource "google_compute_region_network_endpoint_group" "<%= ctx[:primary_resource_id] %>" {
  name                  = "<%= ctx[:vars]['neg_name'] %>"
  network_endpoint_type = "SERVERLESS"
  region                = "us-central1"
  app_engine {
    service = google_app_engine_flexible_app_version.<%= ctx[:primary_resource_id] %>.service
    version = google_app_engine_flexible_app_version.<%= ctx[:primary_resource_id] %>.version_id
  }
}

resource "google_app_engine_flexible_app_version" "<%= ctx[:primary_resource_id] %>" {
  version_id = "v1"
  service    = "appengine-network-endpoint-group"
  runtime    = "nodejs"

  entrypoint {
    shell = "node ./app.js"
  }

  deployment {
    zip {
      source_url = "https://storage.googleapis.com/${google_storage_bucket.<%= ctx[:primary_resource_id] %>.name}/${google_storage_bucket_object.<%= ctx[:primary_resource_id] %>.name}"
    }
  }

  liveness_check {
    path = "/"
  }

  readiness_check {
    path = "/"
  }

  env_variables = {
    port = "8080"
  }

  handlers {
    url_regex        = ".*\\/my-path\\/*"
    security_level   = "SECURE_ALWAYS"
    login            = "LOGIN_REQUIRED"
    auth_fail_action = "AUTH_FAIL_ACTION_REDIRECT"

    static_files {
      path = "my-other-path"
      upload_path_regex = ".*\\/my-path\\/*"
    }
  }

  automatic_scaling {
    cool_down_period = "120s"
    cpu_utilization {
      target_utilization = 0.5
    }
  }

  noop_on_destroy = true
}

resource "google_storage_bucket" "<%= ctx[:primary_resource_id] %>" {
  name       = "<%= ctx[:vars]['neg_name'] %>"
}

resource "google_storage_bucket_object" "<%= ctx[:primary_resource_id] %>" {
  name      = "hello-world.zip"
  bucket    = google_storage_bucket.<%= ctx[:primary_resource_id] %>.name
  source    = "./test-fixtures/appengine/hello-world.zip"
}
